package com.leetcode.math;


/**
 * date:
 * description:
 */
public class Add36 {
    public static void main(String[] args) {
        String a = "1b";
        String b = "2x";
        String res = add36(a, b);
        System.out.println(res);
    }

    private static String add36(String a, String b) {
        int i = a.length() - 1;
        int j = b.length() - 1;
        boolean carry = false;
        StringBuilder sb = new StringBuilder();
        while(i >= 0 || j >= 0 || carry){
            int x = i >= 0 ? getInt(a.charAt(i)) : 0;
            int y = j >= 0 ? getInt(b.charAt(j)) : 0;
            int sum = carry ? x + y + 1 : x + y;
            int cur = sum % 36;
            carry = sum >= 36;
            sb.append(getChar(cur));
            i--;
            j--;
        }
        return sb.reverse().toString();
    }

    private static char getChar(int cur) {
        if(cur < 10 && cur >= 0){
            return (char) ('0' + cur);
        }else{
            return (char) ('a' + cur);
        }
    }

    private static int getInt(char ch) {
        if(Character.isDigit(ch)){
            return ch - '0';
        }else{
            return ch - 'a' + 10;
        }
    }
}
